<?php

class UserInterestedController extends Controller {
    public function beforeaction() {
        if (!isset(Yii::app()->request->cookies['rvhlayout'])) {
            $cookie = new  CHttpCookie('layout', '//layouts/main');
            $cookie->expire = time() + 60 * 60 * 24 * 10; //10 days
            Yii::app()->request->cookies['rvhlayout'] = $cookie;
        }
        $cookie = Yii::app()->request->cookies['rvhlayout'];
        $this->layout = $cookie->value;
        return true;
    }

    public function actionIndex() {
        $location = isset($_GET['location']) ? $_GET['location'] : 0;
        $ip = TreTodayCommon::getUserIp();
        $interesteds = UserInterested::model()->findAllByAttributes(array('user_ip' => $ip));
        $cat = '';
        foreach ($interesteds as $k => $interested) {
            $cat .= $k == count($interesteds) - 1 ? "$interested->category_id" : "$interested->category_id,";
        }

        $model = Product::model()->getForInterested($location, $cat);

        #region SEO
        Yii::app()->clientScript->registerLinkTag('canonical', null, TreTodayCommon::getCurrentUrl());
        Yii::app()->clientScript->registerMetaTag("Các rao vặt bạn có thể quan tâm", 'description');
        Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'], 'keywords');
        $this->pageTitle = 'Rao vặt hot | Có thể bạn quan tâm';
        #endregion SEO

        $this->render('index', array(
            'model' => $model,
            'have_interested' => count($interesteds) > 0
        ));

    }

    public function actionCategory() {
        Yii::app()->language = 'vi';
        if (!isset($_GET['cat_id'])) throw new CHttpException(999, 'Invalid request');
        $cat_id = $_GET['cat_id'];
        $location = isset($_GET['location']) ? $_GET['location'] : 0;
        $model = Product::model()->getForInterested($location, $cat_id);

        $category = Category::model()->findByPk($cat_id);
        if ($category != null) {
            Yii::app()->clientScript->registerMetaTag($category->description, 'description');
            $this->pageTitle = 'Rao vặt hot | Có thể bạn quan tâm | ' . $category->cat_name;
        } else {
            Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_description'], 'keywords');
            $this->pageTitle = 'Rao vặt hot | Có thể bạn quan tâm';
        }
        #region SEO
        Yii::app()->clientScript->registerLinkTag('canonical', null, TreTodayCommon::getCurrentUrl());

        Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'], 'keywords');
        #endregion SEO

        #region user interested
        $interested = UserInterested::model()->findByAttributes(array('user_ip' => TreTodayCommon::getUserIp(), 'category_id' => $cat_id));
        if ($interested == null) {
            $interested = new UserInterested();
            $interested->category_id = $cat_id;
            $interested->priority = 1;
            $interested->user_id = Yii::app()->user->id;
            $interested->user_ip = TreTodayCommon::getUserIp();
            $interested->save();
        } else {
            $interested->priority++;
            $interested->save();
        }
        #endregion

        $this->render('category', array(
            'model' => $model,
            'have_interested' => true,
            'category' => $category
        ));

    }

}